// miniprogram/pages/mine/point/createPoint/createPoint.js
const app = getApp()
var common = require('../../../../utils/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pointTypeArray: [
    ],
    pointTypeIndex: 0,
    pointData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getPointType()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getPointType: function () {
    var that = this
    wx.request({
      url: `${common.baseUrl()}/pointType/findUserPointType/1/100`,
      method: "GET",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      data: {
        query: ""
      },
      success(res) {
        console.log(res)
        if (res.data.flag) {
          var pointData = that.data.pointData
          var pointTypeArray = res.data.data.list
          pointData.point = pointTypeArray[that.data.pointTypeIndex].point
          pointData.pointTypeId = pointTypeArray[that.data.pointTypeIndex].id
          pointData.pointProductId = 0

          that.setData({
            pointData: pointData,
            pointTypeArray: pointTypeArray
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  savePoint: function (e) {
    var that = this
    if (!that.data.pointData.content) {
      wx.showToast({
        title: '内容不能为空',
        image: '/images/warning.png'
      })
      return
    }

    var pointData = that.data.pointData
    pointData.userId = app.globalData.userInfo.id
    pointData.wmember = app.globalData.userInfo.realName
    wx.request({
      url: `${common.baseUrl()}/pointDetail/save`,
      method: "POST",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      data: pointData,
      success(res) {
        console.log(res)
        if (res.data.flag) {
          var pages = getCurrentPages(); //当前页面栈
          if (pages.length > 1) {
            var beforePage = pages[pages.length - 2] //获取上一个页面实例对象
            beforePage.onPullDownRefresh() //触发父页面中的方法
            wx.navigateBack()
          }
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  contentInput: function (e) {
    var that = this
    var pointData = that.data.pointData
    pointData.content = e.detail.value
    that.setData({
      pointData: pointData
    })
    console.log(pointData)
  },
  remarkInput: function (e) {
    var that = this
    var pointData = that.data.pointData
    pointData.remark = e.detail.value
    that.setData({
      pointData: pointData
    })
    console.log(pointData)
  },
  pointTypeChange: function (e) {
    var that = this
    var pointData = that.data.pointData
    pointData.pointTypeId = that.data.pointTypeArray[e.detail.value].id
    pointData.point = that.data.pointTypeArray[e.detail.value].point
    that.setData({
      pointTypeIndex: e.detail.value,
      pointData: pointData
    })
    console.log(pointData)
  },
  addPointClick: function (e) {
    if (!app.globalData.userInfo) {
      app.wxLogin()
      app.userInfoReadyCallback = userInfo => {
        //用户信息保存到全局
        app.globalData.userInfo = userInfo
        //正常操作逻辑
        this.savePoint()
      }
    } else {
      this.savePoint()
    }
  }
})